package com.study.controller;

import com.study.bean.Courseselection;
import com.study.bean.Students;
import com.study.bean.Courses;
import com.study.service.CourseselectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/api/courseselection")
public class CourseselectionController {

    @Autowired
    private CourseselectionService courseselectionService;

    // 查询所有选课记录
    @GetMapping
    public ResponseEntity<List<Courseselection>> getAllCourseselections() {
        List<Courseselection> courseselections = courseselectionService.list();
        return new ResponseEntity<>(courseselections, HttpStatus.OK);
    }

    // 根据学生ID查询选课记录
    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<Courseselection>> getCourseselectionsByStudentId(@PathVariable int studentId) {
        List<Courseselection> courseselections = courseselectionService.listByIds(Collections.singleton(studentId));
        return new ResponseEntity<>(courseselections, HttpStatus.OK);
    }

    // 根据课程ID查询选课记录
    @GetMapping("/course/{courseId}")
    public ResponseEntity<List<Courseselection>> getCourseselectionsByCourseId(@PathVariable int courseId) {
        List<Courseselection> courseselections = courseselectionService.listByIds(Collections.singleton(courseId));
        return new ResponseEntity<>(courseselections, HttpStatus.OK);
    }

    // 添加选课记录
    @PostMapping
    public ResponseEntity<String> addCourseselection(@RequestBody Courseselection courseselection) {
        boolean success = courseselectionService.addCourseselection(courseselection);
        if (success) {
            return new ResponseEntity<>("选课记录添加成功", HttpStatus.CREATED);
        } else {
            return new ResponseEntity<>("选课记录添加失败", HttpStatus.BAD_REQUEST);
        }
    }

    // 删除选课记录
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteCourseselection(@PathVariable int id) {
        boolean success = courseselectionService.removeByIds(Collections.singleton(id));
        if (success) {
            return new ResponseEntity<>("选课记录删除成功", HttpStatus.OK);
        } else {
            return new ResponseEntity<>("选课记录删除失败", HttpStatus.BAD_REQUEST);
        }
    }

    // 更新选课记录
    @PutMapping("/{id}")
    public ResponseEntity<String> updateCourseselection(@PathVariable int id, @RequestBody Courseselection courseselection) {
        boolean success = courseselectionService.updateCourseselection(id, courseselection);
        if (success) {
            return new ResponseEntity<>("选课记录更新成功", HttpStatus.OK);
        } else {
            return new ResponseEntity<>("选课记录更新失败", HttpStatus.BAD_REQUEST);
        }
    }

    // 查询学生选课情况（包括课程信息）
    @GetMapping("/student-courses/{studentId}")
    public <Course> ResponseEntity<List<Course>> getStudentCourses(@PathVariable int studentId) {
        List<Course> courses = (List<Course>) courseselectionService.listByIds(Collections.singleton(studentId));
        return new ResponseEntity<>(courses, HttpStatus.OK);
    }

    // 查询课程选课学生情况（包括学生信息）
    @GetMapping("/course-students/{courseId}")
    public <Student> ResponseEntity<List<Student>> getCourseStudents(@PathVariable int courseId) {
        List<Student> students = courseselectionService.getCourseStudents(courseId);
        return new ResponseEntity<>(students, HttpStatus.OK);
    }
}